//
//  DZRegionManager.m
//  DZFoundation
//
//  Created by Darren Zheng on 15/10/14.
//  Copyright © 2015年 Darren Zheng. All rights reserved.
//

#import "DZRegionManager.h"

@interface DZRegionManager ()

@property (nonatomic, strong) FMDatabase *fmdb;
@property (nonatomic, strong) FMDatabaseQueue *databaseQueue;

@end

#define SQL_TEXT_NAME @"regions.sql"
#define DATABASE_NAME @"regions.db"

@implementation DZRegionManager

+ (DZRegionManager *)sharedInstance
{
    static DZRegionManager *sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
    }
    return self;
}

//初始化
-(void)initRegionManager {
    if (![FileManager fileExistAtCacheDirectoryWithName:DATABASE_NAME])
    {
        NSString *strSQL = [NSString stringWithContentsOfFile:[FileManager bundleFilePathWithName:SQL_TEXT_NAME] encoding:NSUTF8StringEncoding error:nil];
        
        self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:[FileManager cacheFilePathWithName:DATABASE_NAME]];
        
        [self.databaseQueue inDatabase:^(FMDatabase *db) {
            [db executeStatements:strSQL];
        }];
        
        NSLog(@"%@",[FileManager cacheFilePathWithName:DATABASE_NAME]);
    }
    else
    {
        self.databaseQueue = [FMDatabaseQueue databaseQueueWithPath:[FileManager cacheFilePathWithName:DATABASE_NAME]];
    }
}

// 公开方法: 获取所有的省
- (NSMutableArray *)getAllProvinces
{
    NSMutableArray *mArrayResults = [NSMutableArray new];
    [self.databaseQueue inDatabase: ^(FMDatabase *db)
     {
         NSString * sql = @"SELECT code, name FROM regions WHERE code < 100 ORDER BY code";
         if ([db hadError]) {
             NSLog(@"DB Error %d: %@", [db lastErrorCode], [db lastErrorMessage]);
         }
         FMResultSet *rs = [db executeQuery:sql];
         while ([rs next])
         {
             DZRegionProvinceModel *model = [DZRegionProvinceModel new];
             model.code = [rs stringForColumnIndex:0];
             model.name = [rs stringForColumnIndex:1];
             [mArrayResults addObject:model];
         }
         [rs close];
     }];
    return mArrayResults;
}

// 公开方法: 根据省code获取相应所有的市
- (NSMutableArray *)getAllCitiesByProvinceCode:(NSString *)strCode
{

    NSMutableArray *mArrayResults = [NSMutableArray new];
    [self.databaseQueue inDatabase: ^(FMDatabase *db)
     {
         NSString * sql = [NSString stringWithFormat:@"SELECT code, name FROM regions WHERE code BETWEEN %@*100 AND %@*100+99 ORDER BY code", strCode, strCode];
         FMResultSet *rs = [db executeQuery:sql];
         while ([rs next])
         {
             DZRegionCityModel *model = [DZRegionCityModel new];
             model.code = [rs stringForColumnIndex:0];
             model.name = [rs stringForColumnIndex:1];
             [mArrayResults addObject:model];
         }
         [rs close];
     }];
    return mArrayResults;
}

@end
